class ReactiveEffect {
    private _fn: any
    constructor(fn) {
        this._fn = fn
    }

    run() {
        activeEffect = this
        return this._fn()
    }
}

const targetMap = new Map()
export function track(target, key) {
    //target->key->dep

    let depsMap = targetMap.get(target)
    if (!depsMap) {
        depsMap = new Map()

        targetMap.set(target, depsMap)
    }
    let dep = depsMap.get(key)
    if (!dep) {
        dep = new Set()
        depsMap.set(key, dep)
    }
    dep.add(activeEffect)
}
let activeEffect



export function trigger(target, key) {
    let depsMap = targetMap.get(target)
    let dep = depsMap.get(key)
    dep.forEach(effect => {
        effect.run()
    })
}
export function effect(fn) {
    //fn

    const _effect = new ReactiveEffect(fn)
    _effect.run()
    return _effect.run.bind(_effect)
}